
const Controller = require('../../core/Controller');
const query = require('../../core/db');


class userController extends Controller {
  constructor() {
    super();
  }
  async getPalaceList(ctx) {
    //const { mixnick, name, telephone, } = ctx.request.body || ctx.request.query;

    //查询宫殿信息
    let sql = 'select * from palace where 1=1';
    let values = [];
    let palace = await query(sql, values).then( results => { return results; });
    //console.log('palace =====', palace);

    //异步查询对应宫殿的商品信息
    let promises = palace.map((item, index) => {
      let value = item.p_g_ids.split(',');
      let zhanwei = [];
      for(let i=0; i<value.length; i++){ zhanwei.push('?'); }
      let sql = 'select * from goods where g_id in('+zhanwei.join()+')';
      return new Promise(( resolve, reject ) => {
        let res = query(sql, value).then( results => { return results; });
        resolve(res)
      });
    });
    let goods = await Promise.all(promises).then(res => {
      //console.log("========", res);
      return res
    });


    //添加宫殿商品列表
    for(let i=0; i<palace.length; i++) {
      palace[i].p_goodsList = goods[i];
    }
    //console.log(palace);
    ctx.response.body = palace;
  }
}

module.exports = new userController;
